<!DOCTYPE html>
<html lang="en">
	<head>
		<title>threejs webgl - materials - hdr environment mapping</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #000;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #000;

				margin: 0px;
				overflow: hidden;
			}
			a { color: #00f }

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}
		</style>
	</head>
	<body>

		<div id="container"></div>
		<div id="info"><a href="http://threejs.org" target="_blank">threejs</a> - High dynamic range (RGBE) Image-based Lighting (IBL)<br />using run-time generated pre-filtered roughness mipmaps (PMREM)<br/>
			Created by Prashant Sharma and <a href="http://clara.io/" target="_blank">Ben Houston</a>.</div>

		<script src="../build/three.js"></script>
		<script src="js/controls/OrbitControls.js"></script>
		<script src="js/loaders/RGBELoader.js"></script>
		<script src="js/loaders/HDRCubeTextureLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script src="js/pmrem/PMREMGenerator.js"></script>
		<script src="js/pmrem/PMREMCubeUVPacker.js"></script>
		<script src="js/libs/dat.gui.min.js"></script>

		<script src="js/postprocessing/EffectComposer.js"></script>
		<script src="js/postprocessing/RenderPass.js"></script>
		<script src="js/postprocessing/MaskPass.js"></script>
		<script src="js/postprocessing/ShaderPass.js"></script>
		<script src="js/shaders/CopyShader.js"></script>
		<script src="js/shaders/FXAAShader.js"></script>
		<script src="js/postprocessing/BloomPass.js"></script>
		<script src="js/shaders/ConvolutionShader.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;
			var params = {
				envMap: "HDR",
				projection: 'normal',
				roughness: 1.0,
				bumpScale: 0.3,
				background: false,
				exposure: 1.0,
				side: 'front'
			};
			var camera, scene, renderer, controls, objects = [];
			var hdrCubeMap;
			var composer;
			var standardMaterial, floorMaterial;
			var ldrCubeRenderTarget, hdrCubeRenderTarget, rgbmCubeRenderTarget;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set( 0.0, 40, 40 * 3.5 );

				scene = new THREE.Scene();

				renderer = new THREE.WebGLRenderer( { antialias: false } );
				renderer.setClearColor( new THREE.Color( 0xffffff ) );
				renderer.toneMapping = THREE.LinearToneMapping;

				standardMaterial = new THREE.MeshStandardMaterial( {
					map: null,
					bumpScale: - 0.05,
					color: 0xff4444,
					metalness: 0.5,
					roughness: 1.0,
					shading: THREE.SmoothShading
				} );

				var geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
				var torusMesh1 = new THREE.Mesh( geometry, standardMaterial );
				torusMesh1.position.x = 0.0;
				torusMesh1.castShadow = true;
				torusMesh1.receiveShadow = true;
				scene.add( torusMesh1 );
				objects.push( torusMesh1 );

				floorMaterial = new THREE.MeshStandardMaterial( {
					map: null,
					roughnessMap: null,
					color: 0xffffff,
					metalness: 0.0,
					roughness: 0.0,
					shading: THREE.SmoothShading
				} );

				var planeGeometry = new THREE.PlaneBufferGeometry( 200, 200 );
				var planeMesh1 = new THREE.Mesh( planeGeometry, floorMaterial );
				planeMesh1.position.y = - 50;
				planeMesh1.rotation.x = - Math.PI * 0.5;
				planeMesh1.receiveShadow = true;
				scene.add( planeMesh1 );

				var textureLoader = new THREE.TextureLoader();
				textureLoader.load( "./textures/roughness_map.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 9, 2 );
					standardMaterial.map = map;
					standardMaterial.roughnessMap = map;
					standardMaterial.bumpMap = map;
					standardMaterial.needsUpdate = true;
				} );

				var genCubeUrls = function( prefix, postfix ) {
					return [
						prefix + 'px' + postfix, prefix + 'nx' + postfix,
						prefix + 'py' + postfix, prefix + 'ny' + postfix,
						prefix + 'pz' + postfix, prefix + 'nz' + postfix
					];
				};

				var hdrUrls = genCubeUrls( "./textures/cube/pisaHDR/", ".hdr" );
				new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, hdrUrls, function ( hdrCubeMap ) {

					var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
					pmremGenerator.update( renderer );

					var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
					pmremCubeUVPacker.update( renderer );

					hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;

				} );

				var ldrUrls = genCubeUrls( "./textures/cube/pisa/", ".png" );
				new THREE.CubeTextureLoader().load( ldrUrls, function ( ldrCubeMap ) {

					ldrCubeMap.encoding = THREE.GammaEncoding;

					var pmremGenerator = new THREE.PMREMGenerator( ldrCubeMap );
					pmremGenerator.update( renderer );

					var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
					pmremCubeUVPacker.update( renderer );

					ldrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;

				} );


				var rgbmUrls = genCubeUrls( "./textures/cube/pisaRGBM16/", ".png" );
				new THREE.CubeTextureLoader().load( rgbmUrls, function ( rgbmCubeMap ) {

					rgbmCubeMap.encoding = THREE.RGBM16Encoding;

					var pmremGenerator = new THREE.PMREMGenerator( rgbmCubeMap );
					pmremGenerator.update( renderer );

					var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
					pmremCubeUVPacker.update( renderer );

					rgbmCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;

				} );

				// Lights

				scene.add( new THREE.AmbientLight( 0x222222 ) );

				var spotLight = new THREE.SpotLight( 0xffffff );
				spotLight.position.set( 50, 100, 50 );
				spotLight.angle = Math.PI / 7;
				spotLight.penumbra = 0.8;
				spotLight.castShadow = true;
				scene.add( spotLight );

				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.shadowMap.enabled = true;
				container.appendChild( renderer.domElement );

				//renderer.toneMapping = THREE.ReinhardToneMapping;
				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				stats = new Stats();
				container.appendChild( stats.dom );

				controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.target.set( 0, 0, 0 );
				controls.update();

				window.addEventListener( 'resize', onWindowResize, false );

				var gui = new dat.GUI();

				gui.add( params, 'envMap', [ 'None', 'LDR', 'HDR', 'RGBM16' ] );
				gui.add( params, 'roughness', 0, 1 );
				gui.add( params, 'bumpScale', - 1, 1 );
				gui.add( params, 'exposure', 0.1, 2 );
				gui.add( params, 'side', [ 'front', 'back', 'double' ] );
				gui.open();

			}

			function onWindowResize() {

				var width = window.innerWidth;
				var height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				renderer.setSize( width, height );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				stats.begin();
				render();
				stats.end();

			}

			function render() {

				if ( standardMaterial !== undefined ) {

					standardMaterial.roughness = params.roughness;
					standardMaterial.bumpScale = - 0.05 * params.bumpScale;

					var newEnvMap = standardMaterial.envMap;

					switch( params.envMap ) {

						case 'None': newEnvMap = null; break;
						case 'LDR': newEnvMap = ldrCubeRenderTarget ? ldrCubeRenderTarget.texture : null; break;
						case 'HDR': newEnvMap = hdrCubeRenderTarget ? hdrCubeRenderTarget.texture : null; break;
						case 'RGBM16': newEnvMap = rgbmCubeRenderTarget ? rgbmCubeRenderTarget.texture : null; break;

					}

					if( newEnvMap !== standardMaterial.envMap ) {

						standardMaterial.envMap = newEnvMap;
						standardMaterial.needsUpdate = true;
						floorMaterial.emissive = new THREE.Color( 1, 1, 1 );
						floorMaterial.emissiveMap = newEnvMap;
						floorMaterial.needsUpdate = true;

					}


					var side = standardMaterial.side;

					switch ( params.side ) {

						case 'front': side = THREE.FrontSide; break;
						case 'back': side = THREE.BackSide; break;
						case 'double': side = THREE.DoubleSide; break;
	
					}

					if( standardMaterial.side !== side ) {
						
						standardMaterial.side = side;
						standardMaterial.needsUpdate = true;

					}
				}

				renderer.toneMappingExposure = Math.pow( params.exposure, 4.0 );

				var timer = Date.now() * 0.00025;

				camera.lookAt( scene.position );

				for ( var i = 0, l = objects.length; i < l; i ++ ) {

					var object = objects[ i ];
					object.rotation.y += 0.005;

				}

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
